package com.cms.util;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.cms.bean.CategoryBean;
import com.cms.bean.SysConfigBean;
import com.cms.biz.category.CategoryHelper;
import com.cms.database.DBHelper;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

public class ApplicationHelper {

	public static String DES_KEY = "ourhblue";
    public static SysConfigBean getApplicationSysConfigBean(HttpSession session) {
    	ServletContext application = session.getServletContext();
        if (application.getAttribute("ApplicationSysConfigBean") == null) {
            Connection conn = DBHelper.getConn();
            SysConfigBean scb = null;
            try {
                scb = SysConfHelper.getSysConfigBean(conn);
                application.setAttribute("ApplicationSysConfigBean", scb);
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
            	DBHelper.closeConn(conn);
            }
            return scb;
        } else {
            return (SysConfigBean) application.getAttribute("ApplicationSysConfigBean");
        }
    }
    
    
    @SuppressWarnings("finally")
	public static List<CategoryBean> getApplicationSubCategoryListById(HttpSession session,int categoryTypeId) {
    	ServletContext application = session.getServletContext();
    	Hashtable mpHashtable = null;
        if (application.getAttribute("SubCategoryMap") == null) {
        	mpHashtable = new Hashtable();
        }else{
        	mpHashtable = (Hashtable)application.getAttribute("SubCategoryMap");
        }
        if(mpHashtable.size()==0||!mpHashtable.containsKey(String.valueOf(categoryTypeId))){
            Connection conn = DBHelper.getConn();
            List<CategoryBean> list = null;
            try {
                list = CategoryHelper.getCategoryListByTypeId(conn, categoryTypeId);
                mpHashtable.put(String.valueOf(categoryTypeId), list);
                application.setAttribute("SubCategoryMap", mpHashtable);
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
            	DBHelper.closeConn(conn);
            	return list;
            }	
        }else{
        	return (List<CategoryBean>)mpHashtable.get(String.valueOf(categoryTypeId));
        }
    }
    
    public static void setApplicationSubCategoryListById(HttpSession session,List<CategoryBean> list,int categoryTypeId){
    	ServletContext application = session.getServletContext();
    	Hashtable mpHashtable = null;
        if (application.getAttribute("SubCategoryMap") == null) {
        	mpHashtable = new Hashtable();
        }else {
        	mpHashtable = (Hashtable)application.getAttribute("SubCategoryMap");
		}
        if(mpHashtable.containsKey(String.valueOf(categoryTypeId))){
        	mpHashtable.remove(String.valueOf(categoryTypeId));
        }
        mpHashtable.put(String.valueOf(categoryTypeId), list);
    }
}
